//
//  LHAreaView.m
//  Knight_Tour
//
//  Created by Bruce_Lin on 15/5/19.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHAreaView.h"
#import "LHAreaCollectionViewCell.h"
#import "LHAreaModel.h"
#import "UIImageView+WebCache.h"
#import "UIScrollView+MJRefresh.h"

@implementation LHAreaView

- (void)dealloc
{
    [_areaCollectonView release];
    [_freshArr release];
    [_AreaModelArr release];
    [super dealloc];
}

- (void)setAreaModelArr:(NSArray *)AreaModelArr
{
    if (_AreaModelArr != AreaModelArr) {
        [_AreaModelArr release];
        _AreaModelArr = [AreaModelArr retain];
    }
    [_areaCollectonView reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.freshArr = [NSMutableArray array];
        [self creatView];
        
    }
    return self;
}

- (void)creatView
{
    UICollectionViewFlowLayout *fly = [[UICollectionViewFlowLayout alloc]init];
    [fly setItemSize:CGSizeMake(LHWidth/3 - 20, (LHheight - 64) / 4)];
    [fly setSectionInset:UIEdgeInsetsMake(5, 10, 5, 10)];
    
    self.areaCollectonView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:fly];
    _areaCollectonView.delegate = self;
    _areaCollectonView.dataSource = self;
    self.areaCollectonView.backgroundColor = [UIColor colorWithRed:0.871 green:0.962 blue:0.965 alpha:1.000];
    [self addSubview:_areaCollectonView];
    
//    for (NSInteger i = 0; i < self.AreaModelArr.count / 2; i ++) {
//        [self.freshArr addObject:self.AreaModelArr[i]];
//    }
    
    //上拉加载
//    [_areaCollectonView addFooterWithTarget:self action:@selector(refreash)];
    
    [_areaCollectonView registerClass:[LHAreaCollectionViewCell class] forCellWithReuseIdentifier:@"areaReuse"];
    [fly release];
    [_areaCollectonView release];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _AreaModelArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LHAreaCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"areaReuse" forIndexPath:indexPath];
    [cell.areaPic sd_setImageWithURL:[NSURL URLWithString:[self.AreaModelArr[indexPath.item]pic]] placeholderImage:[UIImage imageNamed:@"word4"]];
    cell.areaName.text = [self.AreaModelArr[indexPath.item] namec];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate jumpToDetailsWithIndexPath:indexPath];
}

- (void)refreash
{
    for (NSInteger i = _freshArr.count; i < _AreaModelArr.count; i ++) {
        [_freshArr addObject:_AreaModelArr[i]];
    }
    [_areaCollectonView reloadData];
    [_areaCollectonView footerEndRefreshing];
}


@end
